<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Vote
 *
 * @property int $id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property int $article_id
 * @property int $movie_id
 * @property int $show_id
 * @property int $visit
 * @property float $score
 * @property int $vote_count
 * @property-read \App\Article $article
 * @property-read \App\Movie $movie
 * @property-read \App\Show $show
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereArticleId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereMovieId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereScore($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereShowId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereVisit($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereVoteCount($value)
 * @mixin \Eloquent
 * @property int $favorate_count
 * @property string $deleted_at
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Vote whereFavorateCount($value)
 */
class Vote extends Model
{
    protected $fillable = [
        'article_id', 'show_id', 'movie_id','visit','score','user_id','cookie','ip_address'
    ];


    public function article() {
        return $this->belongsTo('App\Article');
    }
    public function show() {
        return $this->belongsTo('App\Show');
    }
    public function movie() {
        return $this->belongsTo('App\Movie');
    }


    public function setScoreAttribute($value) {
        $totalScore = $this->vote_count * $this->score + $value;
        $vote_count = $this->vote_count + 1;
        $this->vote_count = $vote_count;
        $this->attributes['score'] = $totalScore / $vote_count;
    }
}
